package com.ruoyi.Gzmx_device.controller;

import com.ruoyi.Gzmx_device.domain.Gzmx_device;
import com.ruoyi.Gzmx_device.service.IGzmx_deviceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdw;
import com.ruoyi.gzmx_scdw.service.IGzmx_scdwService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 设备管理表Controller
 * 
 * @author ruoyi
 * @date 2022-10-20
 */
@Controller
@RequestMapping("/Gzmx_device/Gzmx_device")
public class Gzmx_deviceController extends BaseController
{
    private String prefix = "Gzmx_device/Gzmx_device";

    @Autowired
    private IGzmx_deviceService gzmx_deviceService;
    
    @Autowired
    private IGzmx_scdwService gzmx_scdwService;
    
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("Gzmx_device:Gzmx_device:view")
    @GetMapping()
    public String Gzmx_device(ModelMap mmap)
    {
    	
    	return prefix + "/Gzmx_device";
    }

    /**
     * 查询设备管理表列表
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Gzmx_device gzmx_device)
    {
    	SysUser currentUser = ShiroUtils.getSysUser();
    	System.out.println("currentUser--"+currentUser);
    	startPage();
        List<Gzmx_device> list = gzmx_deviceService.selectGzmx_deviceList(gzmx_device);
        return getDataTable(list);
    }

    /**
     * 导出设备管理表列表
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:export")
    @Log(title = "设备管理表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Gzmx_device gzmx_device)
    {
        List<Gzmx_device> list = gzmx_deviceService.selectGzmx_deviceList(gzmx_device);
        ExcelUtil<Gzmx_device> util = new ExcelUtil<Gzmx_device>(Gzmx_device.class);
        return util.exportExcel(list, "设备管理表数据");
    }

    /**
     * 新增设备管理表
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	SysUser currentUser = ShiroUtils.getSysUser();
    	mmap.put("currentUser", currentUser);
        SysDept dept=deptService.selectDeptById(currentUser.getDeptId());
        mmap.put("dept", dept);
    	return prefix + "/add";
    }

    /**
     * 新增保存设备管理表
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:add")
    @Log(title = "设备管理表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Gzmx_device gzmx_device)
    {

    	Date dy=new Date();
    	gzmx_device.setCreateTime(dy); 
    	SysUser currentUser = ShiroUtils.getSysUser();
    	gzmx_device.setCreateBy(currentUser.getUserName());
    	gzmx_device.setZyq_id(gzmx_device.getDev_azdw2_id());
    	gzmx_device.setZyq_name(gzmx_device.getDev_azdw2_name());
    	Gzmx_scdw gzmx_scdw=gzmx_scdwService.selectGzmx_scdwById(gzmx_device.getDev_azdw2_id());
    	
    	String dwmc_jp=gzmx_scdw.getDwmc_jp();
    	gzmx_device.setDev_azqy_id(gzmx_scdw.getDept_id_id());
    	gzmx_device.setDev_azqy_name(gzmx_device.getDev_azdw2_name());
    	SysDept dept=deptService.selectDeptById(currentUser.getDeptId());

    	
    	if(dept.getAncestors().split(",").length>4) {
    		gzmx_device.setBm_id(currentUser.getDept().getDeptId());
    		gzmx_device.setBm_name(currentUser.getDept().getDeptName());
    		gzmx_device.setDeptId(currentUser.getDept().getDeptId());
    		gzmx_device.setDept_name(currentUser.getDept().getDeptName());
    		gzmx_device.setSsdw_dm(currentUser.getDept().getParentId().toString());
    		SysDept dept_new=deptService.selectDeptById(currentUser.getDept().getParentId());
    		gzmx_device.setSsdw_name(dept_new.getDeptName());
    		
    	}else {
    		gzmx_device.setBm_id(currentUser.getDept().getDeptId());
    		gzmx_device.setBm_name(currentUser.getDept().getDeptName());
    		gzmx_device.setDeptId(currentUser.getDept().getDeptId());
    		gzmx_device.setDept_name(currentUser.getDept().getDeptName());
    		gzmx_device.setSsdw_dm(currentUser.getDept().getDeptId().toString());
    		gzmx_device.setSsdw_name(currentUser.getDept().getDeptName());
    	}
    	Gzmx_device gzmx_device_new=new Gzmx_device();
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");  
    	String str_dev_ybbh=gzmx_scdw.getDeptId()+"-"+dwmc_jp+"-"+formatter.format(gzmx_device.getDev_tcrq());
    	gzmx_device_new.setStr_dev_ybbh(str_dev_ybbh);
    	List<Gzmx_device> list=gzmx_deviceService.selectGzmx_deviceList(gzmx_device_new);
    	Long ls=0L;
    	if(list.size()>0) {    		
    		 ls=Long.parseLong(list.get(0).getDev_ybbh().split("-")[3]);
    	}else {    		
    	}
        String yybh_ls="";
        
		if(String.valueOf(ls+1).length()==1) {yybh_ls="000"+String.valueOf(ls+1);}
		if(String.valueOf(ls+1).length()==2) {yybh_ls="00"+String.valueOf(ls+1);}
		if(String.valueOf(ls+1).length()==3) {yybh_ls="0"+String.valueOf(ls+1);}
		if(String.valueOf(ls+1).length()==4) {yybh_ls=String.valueOf(ls+1);}
		gzmx_device.setDev_ybbh(str_dev_ybbh+"-"+yybh_ls);
    	return toAjax(gzmx_deviceService.insertGzmx_device(gzmx_device));
    }

    /**
     * 修改设备管理表
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Gzmx_device gzmx_device = gzmx_deviceService.selectGzmx_deviceById(id);
        mmap.put("gzmx_device", gzmx_device);
        SysUser currentUser = ShiroUtils.getSysUser();
    	mmap.put("currentUser", currentUser);
        SysDept dept = deptService.selectDeptById(currentUser.getDeptId());
        mmap.put("dept", dept);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理表
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:edit")
    @Log(title = "设备管理表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Gzmx_device gzmx_device)
    {
        return toAjax(gzmx_deviceService.updateGzmx_device(gzmx_device));
    }

    /**
     * 删除设备管理表
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:remove")
    @Log(title = "设备管理表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gzmx_deviceService.deleteGzmx_deviceByIds(ids));
    }

    /**
     * 模型管理-模型设计 源设备列表查询
     */
    @RequiresPermissions("Gzmx_device:Gzmx_device:list")
    @PostMapping("/template-device-list")
    @ResponseBody
    public AjaxResult templateDeviceList(@RequestBody Gzmx_device gzmx_device)
    {
        List<Gzmx_device> list = gzmx_deviceService.selectGzmx_deviceList(gzmx_device);
        return AjaxResult.success(list);
    }

}
